﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VirtualOrg.DB;
using VirtualOrg.Helper;
using System.Web.UI.HtmlControls;
using Obout.Ajax.UI.FileUpload;

namespace VirtualOrg.Web
{
    public partial class FormUpdatePhoto : OboutInc.oboutAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.Cookies.AllKeys.Contains(SiteConstants.CookieName))
            {
                Response.Redirect("~/User/Login.aspx");
            }

            foreach (PostedFileInfo info in fileProfilePic.PostedFiles)
            {
                long userId=SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID;
                string folderPath="~/Content/Photos/ProfilePic";
                string fileName = string.Format("u{0}.jpg", userId);
                info.SaveAs(Server.MapPath(string.Format("{0}/{1}", folderPath, fileName)));
                using (DbStoreDataContext dataContext = new DbStoreDataContext())
                {
                    dataContext.UpdateUserPhoto(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID, fileName);
                }
            }

            if (!IsPostBack)
            {
                GetUserInfo();
            }
        }

        private void GetUserInfo()
        {
            using (DbStoreDataContext dataContext = new DbStoreDataContext())
            {
                var result = dataContext.GetUserInfo(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID);
                foreach (var item in result)
                {
                    lblTitle.InnerText = item.FullName;
                    imgProfilePic.Src = string.Format("../Content/Photos/ProfilePic/{0}", item.ProfilePic);
                }
            }
        }
    }
}